package org.dyk.mallmanagementsystem.service.goods;

import org.dyk.mallmanagementsystem.model.Goods;
import org.dyk.mallmanagementsystem.model.Orders;
import org.dyk.mallmanagementsystem.repository.GoodsRepository;
import org.dyk.mallmanagementsystem.repository.OrdersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;


@Service
public class GoodsServiceImpl implements GoodsService{
    @Autowired
    private GoodsRepository goodsRepository;
    @Autowired
    private OrdersRepository ordersRepository;

    /**
     * 商品页面 分页
     * @param pageable
     * @return
     */
    @Override
    public Page<Goods> findAll(Pageable pageable) {
        return  goodsRepository.findAll(pageable);
    }

    /**
     * 查询所有商品
     * @return
     */
    @Override
    public List<Goods> findAll() {
        return goodsRepository.findAll();
    }

    /**
     * 根据 主键 查询商品
     * @param gid
     * @return
     */
    @Override
    public Goods findByGid(int gid) {
        return goodsRepository.findByGid(gid);
    }

    /**
     * 添加商品
     * @param tid
     * @param goodsName
     * @param goodsPrice
     * @param goodsDescribe
     * @param goodsImg
     * @return
     */
    @Override
    public boolean insert(int tid, String goodsName, int goodsStock, String goodsPrice, String goodsDescribe, MultipartFile goodsImg) {
        //将上传图片存入本地
        String originalFilename = goodsImg.getOriginalFilename();
        try {
            goodsImg.transferTo(new File("F:\\学习文件\\毕业设计\\mall-management-system\\src\\main\\resources\\static\\img\\" + originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //拼接图片路径,并存入数据库
        String goodsImgUrl ="/admin/img/" + goodsImg.getOriginalFilename();
        //判断是否有重复商品名
        Goods byGoodsName = goodsRepository.findByGoodsName(goodsName);
        if (byGoodsName == null){
            //验证通过，添加数据到数据库
            Goods goods = new Goods();
            goods.setGoodsName(goodsName);
            goods.setTid(tid);
            goods.setGoodsStock(goodsStock);
            goods.setGoodsPrice(goodsPrice);
            goods.setGoodsDescribe(goodsDescribe);
            goods.setGoodsImg(goodsImgUrl);
            goodsRepository.save(goods);
            return true;
        }else {
            return false;
        }
    }

    /**
     * 修改商品
     * @param gid
     * @param tid
     * @param goodsName
     * @param goodsPrice
     * @param goodsDescribe
     * @return
     */
    @Override
    public boolean updata(int gid, int tid, String goodsName, int goodsStock, String goodsPrice, String goodsDescribe) {
        try{
            //没有重复商品名
            Goods byGoodsName = goodsRepository.findByGid(gid);
            byGoodsName.setGid(gid);
            byGoodsName.setTid(tid);
            byGoodsName.setGoodsName(goodsName);
            byGoodsName.setGoodsStock(goodsStock);
            byGoodsName.setGoodsPrice(goodsPrice);
            byGoodsName.setGoodsDescribe(goodsDescribe);
            goodsRepository.save(byGoodsName);
            return true;
        }catch (Exception e){
            //有重复商品名
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 修改商品图片
     * @param gid
     * @param goodsImg
     * @return
     */
    @Override
    public boolean updataGoodsImg(int gid, MultipartFile goodsImg) {
        //获取图片名称
        String originalFilename = goodsImg.getOriginalFilename();
        //将图片存入本地
        try {
            goodsImg.transferTo(new File("F:\\学习文件\\毕业设计\\mall-management-system\\src\\main\\resources\\static\\img\\" + originalFilename));
            //将图片路径存入数据库
            String goodsImgUrl = "/admin/img/" + originalFilename;
            Goods byGid = goodsRepository.findByGid(gid);
            byGid.setGoodsImg(goodsImgUrl);
            goodsRepository.save(byGid);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除商品
     * @param gid
     * @return
     */
    @Override
    public boolean delete(int gid) {
        //验证当前商品下是否还有订单
        List<Orders> byGid = ordersRepository.findByGid(gid);
        if (byGid.isEmpty()){
            Goods goods = goodsRepository.findByGid(gid);
            goodsRepository.delete(goods);
            return true;
        }else {
            return false;
        }
    }

    /**
     * 自定义查询商品
     * @param goodsName
     * @param tid
     * @return
     */
    @Override
    public List<Goods> selectByGoods(String goodsName, int tid) {
        return goodsRepository.selectByGoods(goodsName,tid);
    }
}
